﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BatchReplace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择文件夹路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "请选择文件夹";
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            if (txtDirName.Text.Trim().Length > 0)
            {
                folderBrowserDialog1.SelectedPath = txtDirName.Text.Trim();
            }
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtDirName.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        /// <summary>
        /// 替换前确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //指定文件夹路径
            string dirName = txtDirName.Text.Trim();
            if (dirName.Length == 0)
            {
                CommonTips("请先选择文件夹路径！");
                return;
            }

            //被替换的值
            string oldName = txtOldName.Text.Trim();
            if (oldName.Length == 0)
            {
                CommonTips("请输入需要替换的名称！");
                txtOldName.Focus();
                return;
            }

            //新值
            string newName = txtNewName.Text.Trim();
            if (newName.Length == 0)
            {
                CommonTips("请输入新的名称！");
                txtNewName.Focus();
                return;
            }

            txtContent.Text = string.Empty;
            //执行替换
            ExecuteReplace(dirName, oldName, newName);
        }

        /// <summary>
        /// 替换指定文件夹下的所有子文件和文件夹
        /// </summary>
        /// <param name="path">指定文件夹路径</param>
        /// <param name="oldValue">被替换的值</param>
        /// <param name="newValue">新值</param>
        private void ExecuteReplace(string path, string oldValue, string newValue)
        {
            try
            {
                //根据路径获取目录
                DirectoryInfo dir = new DirectoryInfo(path);
                if (dir == null) return;

                //获取该目录下所有子文件
                FileInfo[] fileList = dir.GetFiles();
                for (int i = 0; i < fileList.Length; i++)
                {
                    FileInfo fileInfo = fileList[i];
                    //读取文件内容
                    string content = File.ReadAllText(fileInfo.FullName);
                    if (content.Contains(oldValue))
                    {
                        txtContent.Text += "开始替换内容：" + fileInfo.FullName + "\r\n";
                        //替换内容
                        content = content.Replace(oldValue, newValue);
                        //保存并关闭文件
                        File.WriteAllText(fileInfo.FullName, content);
                        txtContent.Text += "替换完成！" + "\r\n";
                    }

                    //获取文件名
                    string oldFile = fileInfo.Name;
                    if (oldFile.Contains(oldValue))
                    {
                        txtContent.Text += "开始替换文件名：" + fileInfo.FullName + "\r\n";
                        //替换指定字符串
                        string newFile = oldFile.Replace(oldValue, newValue);
                        //获取文件目录完整路径
                        string fileDir = fileInfo.DirectoryName;
                        //移动文件，实现文件名替换
                        fileInfo.MoveTo(fileDir + "\\" + newFile);
                        txtContent.Text += "替换完成！" + "\r\n";
                    }
                }

                //获取该路径下所有子目录
                DirectoryInfo[] dirList = dir.GetDirectories();
                for (int i = 0; i < dirList.Length; i++)
                {
                    DirectoryInfo dirInfo = dirList[i];
                    //获取目录名称
                    string oldDir = dirInfo.Name;
                    //获取目录完整路径
                    string fullName = dirInfo.FullName;
                    if (oldDir.Contains(oldValue))
                    {
                        txtContent.Text += "开始替换目录名：" + fullName + "\r\n";
                        //替换指定字符串
                        string newDir = oldDir.Replace(oldValue, newValue);
                        //获取父级目录
                        DirectoryInfo parentDir = dirInfo.Parent;
                        //替换后的完整路径
                        fullName = parentDir.FullName + "\\" + newDir;
                        //移动文件夹，实现文件夹名称替换
                        dirInfo.MoveTo(fullName);
                        txtContent.Text += "替换完成！" + "\r\n";
                    }

                    //继续替换其下子目录和文件
                    ExecuteReplace(fullName, oldValue, newValue);
                }
            }
            catch (Exception ex)
            {
                txtContent.Text += ex.Message + "\r\n";
            }
        }

        /// <summary>
        /// 通用提示函数
        /// </summary>
        /// <param name="content"></param>
        private void CommonTips(string content)
        {
            MessageBox.Show(content, "温馨提示");
        }
    }
}
